package com.ophelisis.actions.codeMgmt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.common.consts.ConstCode.DefaultCodeGroup;
import com.ophelisis.persistence.dao.CodeDAO;
import com.ophelisis.persistence.dao.CodeGroupDAO;
import com.ophelisis.persistence.domain.CodeGroupVO;
import com.ophelisis.persistence.domain.CodeVO;

/** <pre>
 * Class : com.ophelisis.actions.codeMgmt.CommonCodeMgmt / CommonCodeMgmt.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 07. 29.
 */
@Controller
public class CommonCodeMgmt {
	@Autowired
	CodeGroupDAO codeGroupDAO;
	
	@Autowired
	CodeDAO codeDAO;
	
	private List<CodeGroupVO> codeGroupList;
	private List<CodeVO> codeList;
	private String sResult = "";
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "CodeMgmt Log> ";
	
	@RequestMapping(value="commonCodeMgmt.action")
	public ModelAndView commonCodeMgmt(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="target", defaultValue="cmMain") String sTarget,
			@RequestParam(value="_groupId", defaultValue="0") String sMenuGroupId,
			@RequestParam(value="_menuId", defaultValue="0") String sMenuId,
			@RequestParam(value="_menuNm", defaultValue="") String sMenuNm,
			@RequestParam(value="_menuAuth", defaultValue="0") String sUserMenuAuth,
			@RequestParam(value="_prevUpCode", defaultValue="") String sPrevUpCode,
			@RequestParam(value="_prevCode", defaultValue="") String sPrevCode,
			@RequestParam(value="isNew", defaultValue="Y") String isNew,
			@ModelAttribute CodeVO codeVO,
			@ModelAttribute CodeGroupVO codeGroupVO) throws Exception {
		ModelAndView mav = new ModelAndView();

		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " CommonCodeMgmt called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");

		try {
			// 공통 요소
			mav.addObject("_groupId", sMenuGroupId);
			mav.addObject("_menuId", sMenuId);
			mav.addObject("_menuNm", sMenuNm);
			mav.addObject("_menuAuth", sUserMenuAuth);
			mav.addObject("_prevUpCode", sPrevUpCode);
			mav.addObject("_prevCode", sPrevCode);
			
			Map<String, Object> params = new HashMap<String, Object>();
			
			if (sTarget.equals("cmMain")) {
				mav = returnMgmt("main", mav, params);
			} else if (sTarget.equals("cmList")) {
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("cmSaveGroup")) {
				try {
					sResult = (isNew.equals("Y") ? codeGroupDAO.setCodeGroupAdd(codeGroupVO) : codeGroupDAO.setCodeGroupModify(codeGroupVO)) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, codeGroupId=" + codeGroupVO.getCodeGroupId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("cmSaveCode")) {
				try {
					sResult = (isNew.equals("Y") ? codeDAO.setCodeAdd(codeVO) : codeDAO.setCodeModify(codeVO)) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, codeId=" + codeVO.getCodeId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("cmDeleteGroup")) {
				try {
					sResult = codeGroupDAO.setCodeGroupDelete(codeGroupVO) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, codeGroupId=" + codeGroupVO.getCodeGroupId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("cmDeleteCode")) {
				try {
					sResult = codeDAO.setCodeDelete(codeVO) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, codeId=" + codeVO.getCodeId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("cmExistCode")) {
				params.clear();
				params.put("codeGroupId", codeGroupVO.getCodeGroupId());
				
				int resultCount = codeDAO.existCode(params);
	
				mav.addObject("result", resultCount);
				
				mav.setViewName("/WEB-INF/jsp/common/ajaxResult.jsp");
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	
	public ModelAndView returnMgmt(
			@RequestParam(value="cmd", defaultValue="list") String cmd,
			@ModelAttribute ModelAndView mav,
			Map<String, Object> params) throws Exception {
		if (cmd.equals("main")) {
			mav.setViewName("/WEB-INF/jsp/codeMgmt/commonCodeMgmt.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " CommonCodeMgmt Main-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("list")) {
			try {
				int iTotalCount = 0;
				iTotalCount = codeGroupDAO.getCodeGroupListCnt(params);
				
				if (iTotalCount == 0) {
					CodeGroupVO codeGroupVO = new CodeGroupVO();
					
					for (int i = 0; i < DefaultCodeGroup.values().length; i++) {
						codeGroupVO.setCodeGroupNm(DefaultCodeGroup.values()[i].getGroupNm());
						codeGroupVO.setCodeGroupDesc(DefaultCodeGroup.values()[i].getGroupDesc());
						codeGroupVO.setUseYn(DefaultCodeGroup.values()[i].getUseYn());
						
						codeGroupDAO.setCodeGroupAdd(codeGroupVO);
					}
				}
				
				codeGroupList = codeGroupDAO.getCodeGroupList(params);
				codeList = codeDAO.getCodeList(params);
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}

			mav.addObject("codeGroupList", codeGroupList);
			mav.addObject("codeList", codeList);
			mav.setViewName("/WEB-INF/jsp/codeMgmt/commonCodeMgmtList.jsp");
			
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " CommonCodeMgmt List-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
		
		return mav;
	}
}

